﻿using CuaHangLego.Bussiness;
using CuaHangLego.DataAccess;
using CuaHangLego.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CuaHangLego.UI
{
    public partial class BoLocUI : Form
    {
        private DataGridView tbl;
        private SanPhamKhoBSS bss;
        IList<SanPhamKho> cur;
        public BoLocUI(DataGridView tbl, SanPhamKhoBSS bss)
        {
            InitializeComponent();
            this.tbl = tbl;
            this.bss = bss;
            this.cur = bss.ListSP;
        }

        private void BoLocUI_Load(object sender, EventArgs e)
        {
            IList<ChuDe> listCD = ChuDeDA.Instance.GetAll();
            listCD.Insert(0, new ChuDe(-1, "Tất cả", ""));
            comboBoxChuDe.DataSource = listCD;
        }

        private void comboBoxChuDe_SelectedIndexChanged(object sender, EventArgs e)
        {
            IList<SanPhamKho> rs = null;
            if (bss.IsShowInVisible)
                rs = cur.Where(sp => (sp.ChuDe.MaSo == (comboBoxChuDe.SelectedItem as ChuDe).MaSo
                || (comboBoxChuDe.SelectedItem as ChuDe).MaSo == -1)
                && sp.SoManhGhep >= numericUpDownSoManh1.Value
                && sp.SoManhGhep <= numericUpDownSoManh2.Value
                && sp.GiaBan >= numericUpDownGia1.Value
                && sp.GiaBan <= numericUpDownGia2.Value
                && sp.NgaySX >= dateNgaySX1.Value
                && sp.NgaySX <= dateNgaySX2.Value
                && sp.BaoHanh >= numericUpDownBaoHanh1.Value
                && sp.BaoHanh <= numericUpDownBaoHanh2.Value
                && sp.SoLuong >= numericUpDownSoLuong1.Value
                && sp.SoLuong <= numericUpDownSoLuong2.Value).ToList();
            else rs = cur.Where(sp => sp.IsVisible==true && (sp.ChuDe.MaSo == (comboBoxChuDe.SelectedItem as ChuDe).MaSo
                || (comboBoxChuDe.SelectedItem as ChuDe).MaSo == -1)
                && sp.SoManhGhep >= numericUpDownSoManh1.Value
                && sp.SoManhGhep <= numericUpDownSoManh2.Value
                && sp.GiaBan >= numericUpDownGia1.Value
                && sp.GiaBan <= numericUpDownGia2.Value
                && sp.NgaySX >= dateNgaySX1.Value
                && sp.NgaySX <= dateNgaySX2.Value
                && sp.BaoHanh >= numericUpDownBaoHanh1.Value
                && sp.BaoHanh <= numericUpDownBaoHanh2.Value
                && sp.SoLuong >= numericUpDownSoLuong1.Value
                && sp.SoLuong <= numericUpDownSoLuong2.Value).ToList();
            tbl.DataSource = rs;
        }

        private void buttonReset_Click(object sender, EventArgs e)
        {
            numericUpDownBaoHanh1.Value = 0;
            numericUpDownBaoHanh2.Value = 100;
            numericUpDownGia1.Value = 0;
            numericUpDownGia2.Value = 5000;
            numericUpDownSoManh1.Value = 0;
            numericUpDownSoManh2.Value = 5000;
            numericUpDownSoLuong1.Value = 0;
            numericUpDownSoLuong2.Value = 1000;
            dateNgaySX1.Value = dateNgaySX1.MinDate;
            dateNgaySX2.Value = DateTime.Now;
            comboBoxChuDe.SelectedIndex = 0;
            tbl.DataSource = cur;
        }
    }
}
